package com.hspedu.springboot.furn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hspedu.springboot.furn.bean.Furn;
import com.hspedu.springboot.furn.service.FurnService;
import com.hspedu.springboot.furn.utils.Result;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/14 1:21
 */
@RestController         // 控制器，同时指定返回json数据给前端
public class FurnController {
    @Resource
    private FurnService furnService;    // 从ioc容器中拿到furnService

    @PostMapping("/save")
    public Result save(@Valid @RequestBody Furn furn, Errors errors) {    // 增加家居
        //@RequestBody会将前端发送的json数据封装成JavaBean
        HashMap<String, Object> map = new HashMap<>();  // 用于封装每个字段的错误信息
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        if (map.isEmpty()) { // map为空则表示没有错误
            furnService.save(furn);     // 增加家居到数据库
            return Result.success();
        } else {     // 有错误
            return Result.fail("400", "验证失败~", map);
        }

    }

    @RequestMapping(value = "/list")
    public Result list() {       // 显示家居信息
        List<Furn> furns = furnService.list();  // 查询数据库中所有数据
        return Result.success(furns);
    }

    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody Furn furn, Errors errors) {         // 更新家居信息
        HashMap<String, Object> map = new HashMap<>();  // 用于封装每个字段的错误信息
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        if (map.isEmpty()) {      // 没有错误，验证通过
            furnService.updateById(furn);
            return Result.success();
        } else {     // 有错误，验证不通过
            return Result.fail("400", "验证失败~", map);
        }

    }

    @GetMapping(value = "/getFurnById/{id}")        // 用于表单回显
    public Result getFurnById(@PathVariable(value = "id") Integer id) {
        Furn furn = furnService.getById(id);
        return Result.success(furn);
    }

    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable(value = "id") Integer id) {   //删除家居
        furnService.removeById(id);
        return Result.success();
    }

    @GetMapping(value = "/listByPage")
    public Result listByPage(@RequestParam(defaultValue = "1") Integer pageNo,  // 无条件分页查询
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<Furn> furnPage = new Page<>(pageNo, pageSize); // 初始化分页
        Page<Furn> page = furnService.page(furnPage);   // 分页查询
        return Result.success(page);
    }
//    @GetMapping(value = "/listByPageCondition")
//    public Result listByPageCondition(@RequestParam(defaultValue = "1") Integer pageNo, //条件分页查询
//                                      @RequestParam(defaultValue = "5") Integer pageSize,
//                                      @RequestParam(defaultValue = "") String search){
//        Page<Furn> furnPage = new Page<>(pageNo, pageSize); //初始化分页
//        QueryWrapper<Furn> queryWrapper = Wrappers.query();   // 设置查询条件
//        queryWrapper.like("name",search);       // 模糊查询
//        Page<Furn> page = furnService.page(furnPage, queryWrapper);
//        return Result.success(page);
//}

    @GetMapping(value = "/listByPageCondition")
    public Result listByPageCondition(@RequestParam(defaultValue = "1") Integer pageNo, //条件分页查询
                                      @RequestParam(defaultValue = "5") Integer pageSize,
                                      @RequestParam(defaultValue = "") String search) {
        Page<Furn> furnPage = new Page<>(pageNo, pageSize);     // 初始化分页
        LambdaQueryWrapper<Furn> lambdaQueryWrapper = Wrappers.lambdaQuery(Furn.class); // 获取lambdaQueryWrapper对象
        lambdaQueryWrapper.like(Furn::getName,search);  // 模糊查询 Furn::getName指的是Furn类中name属性映射的表字段
        Page<Furn> page = furnService.page(furnPage, lambdaQueryWrapper);   // 将条件分页查询到的结果封装到page中
        return Result.success(page);
    }
}
